//
//  JkmQRCodeViewController.swift
//  SinaWb
//
//  Created by Jiang on 5/18/16.
//  Copyright © 2016 Jiang. All rights reserved.
//

import UIKit
import AVFoundation

class JkmQRCodeViewController: UIViewController{
    
    override func loadView() {
        view = JkmQrView(frame: UIScreen.mainScreen().bounds)
    }
    
//MARK: - 设置NavigationBarItem
    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationItem.title = "扫一扫"
        let dic = [NSForegroundColorAttributeName: UIColor.whiteColor()]
        navigationController?.navigationBar.titleTextAttributes = dic
        navigationController?.navigationBar.barTintColor = UIColor.clearColor()
        view.backgroundColor  = UIColor.blackColor()
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: .Plain, target: self, action: #selector(closeTheViewcontroller))
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "相册", style: .Plain, target: self, action: #selector(photos))
        
        //添加并设置TabBar
        SetTabbar()
    }
    
//MARK: - 添加并设置TabBar
    private func SetTabbar(){
        //设置TabBar
        let Tabbar = UITabBar()
        Tabbar.delegate = self
        
        let QrCodeButton  = UITabBarItem(title: "二维码", image: UIImage(named: "qrcode_tabbar_icon_qrcode"),tag: 0)
        
        let MyQrCode = UITabBarItem(title: nil, image: UIImage(named: "MyQrCodeImage"), tag: 2)
        
        let BarCodeButton = UITabBarItem(title: "条纹码", image: UIImage(named: "qrcode_tabbar_icon_barcode"), tag: 1)
        
        
        let itemArray: Array<UITabBarItem> =  [QrCodeButton,MyQrCode,BarCodeButton]
        
        Tabbar.setItems(itemArray, animated: true)
        Tabbar.selectedItem = Tabbar.items?.first
        Tabbar.barTintColor = UIColor.clearColor()
        
        view.addSubview(Tabbar)
        
        Tabbar.snp_makeConstraints { (make) in
            make.bottom.equalTo(view.snp_bottom)
            make.left.equalTo(view.snp_left)
            make.width.equalTo(view.snp_width)
        }
        
    }
    //打开相册
    
    @objc private func photos(){
        //打开相册
        //判断能否打开相册
        if !UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary){
            return
        }
        
        //创建相册控制器
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        //弹出相册控制器
        presentViewController(imagePicker, animated: true, completion: nil)
        
    }
//关闭按钮
  @objc  private func closeTheViewcontroller(){
    
       dismissViewControllerAnimated(true, completion: nil)
    
    }
}


//MARK: - UIImagePickerController 代理
extension JkmQRCodeViewController:UIImagePickerControllerDelegate,UINavigationControllerDelegate{
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]){
     
        guard let image : UIImage = info[UIImagePickerControllerOriginalImage] as? UIImage else{
            return
        }
        //从选中图片中读取二维码
        //1.创建一个探测器
        let detector =  CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: [CIDetectorAccuracy: CIDetectorAccuracyLow])
        //2.利用探测器探测数据
        let result =  detector.featuresInImage(CIImage(image: image)!)
        //3.取出探测到的数据
        for item in result{
           let message = (item as! CIQRCodeFeature).messageString
        
            (view as! JkmQrView).MessageLable.text = message
        }
        
        picker.dismissViewControllerAnimated(true, completion: nil)
        
        
    }
    func imagePickerControllerDidCancel(picker: UIImagePickerController){
        picker.dismissViewControllerAnimated(true, completion: nil)
    }
}


//MARK: - UITabBar代理
extension JkmQRCodeViewController:UITabBarDelegate{
    
    func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        if item.tag == 2 {
            navigationController?.pushViewController(JkmMyQrCode(), animated: true)
        }
        
        
        let aView = view as! JkmQrView
        
        if item.tag == 1{
            aView.backviewHeight = view.frame.width * 0.25
            return
        }
        
        aView.backviewHeight = view.frame.width * 0.5
        
    }
    
}
